
package com.study.happystudy.controller;

import com.study.happystudy.entity.ResultEntity;
import com.study.happystudy.po.Course;
import com.study.happystudy.po.LeaveNote;
import com.study.happystudy.po.User;
import com.study.happystudy.service.impl.CourseServiceImpl;
import com.study.happystudy.service.impl.LeaveNoteServiceImpl;
import com.study.happystudy.service.impl.MessageServiceImpl;
import com.study.happystudy.service.impl.UserServiceImpl;
import com.study.happystudy.tool.ColorChange;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@CrossOrigin
@RestController
@RequestMapping("leavenote/")
public class LeaveNoteController {

    @Resource
    LeaveNoteServiceImpl leaveNoteService;
    @Resource
    CourseServiceImpl courseService;
    @Resource
    MessageServiceImpl messageService;
    @Resource
    UserServiceImpl userService;

    // 查看user和course是否存在

    /**
     * 插入请假记录
     * 默认状态为待审批
     */
    @PostMapping("insertLeaveNote")
    public String insertLeaveNote(@RequestParam int userId,
                                  @RequestParam int courseId,
                                  @RequestParam String startTime,
                                  @RequestParam String endTime,@RequestParam String reason) {


        int res = courseService.isUserOrCourseExist(userId, courseId);
        if(res != 1){
            return ResultEntity.Fail(-1,"userId或courseId不存在");
        }

        Course cc = courseService.findCourseById(courseId);

        User student = userService.findUserById(userId);

        if(!Objects.equals(student.getUsertype(), "学生")){
            return ResultEntity.Fail(-1,"该用户不是学生");
        }

        // 发送两个消息:
        // 给学生发消息
        messageService.sendCourseMessage(courseId,userId,"您在"+cc.getCourseName()+"的假条已经发送给任课老师了");
        // 给老师发消息
        messageService.sendCourseMessage(userId,courseId,"您的学生"+student.getUserNickname()+"在"+cc.getCourseName()+"请假了");

        leaveNoteService.insertLeaveNote(userId, courseId, startTime, endTime,reason);
        return ResultEntity.Success(1, "插入成功");
    }
    @PostMapping("updateLeaveNote")
    public String updateLeaveNote(@RequestParam String approved, @RequestParam int id){
        List<String> states = new ArrayList<>(Arrays.asList("同意","拒绝"));

        if(!states.contains(approved)){
            return ResultEntity.Fail(-1,"状态参数错误");
        }
        try {
            leaveNoteService.updateLeaveNote(approved, id);

            LeaveNote ln = leaveNoteService.getLeaveNoteById(id);

            // 给学生发送消息
            messageService.sendCourseMessage(ln.getCourseId(), ln.getUserId(), "老师" + approved + "了你的假条申请");
        }catch (Exception e){
            ColorChange.Red(e.getMessage());
            return ResultEntity.Fail(-1,"更新失败");
        }

        return ResultEntity.Success(1,"更新成功");
    }

    @GetMapping("getLeaveNoteById")
    public LeaveNote getLeaveNoteById(@RequestParam int id) {
        return leaveNoteService.getLeaveNoteById(id);
    }

    @DeleteMapping("deleteLeaveNoteById")
    public String deleteLeaveNoteById(@RequestParam int id) {

        if (getLeaveNoteById(id) == null) {
            return ResultEntity.Fail(-1, "请假记录不存在");
        }

        try {
            leaveNoteService.deleteLeaveNoteById(id);
        }catch (Exception e){
            ColorChange.Red(e.getMessage());
            return ResultEntity.Fail(-1, "删除失败");
        }

        return ResultEntity.Success(1, "删除成功");
    }

    @GetMapping("getLeaveNotesByCourseId")
    public List<LeaveNote> getLeaveNotesByCourseId(@RequestParam int courseId) {
        try{
            return leaveNoteService.getLeaveNotesByCourseId(courseId);
        } catch (Exception e){
            ColorChange.Red(e.getMessage());
            return null;
        }
    }

    @GetMapping("getCourseLaveNotes")
    public Object getLaveNotes(@RequestParam int courseId,
                               @RequestParam String type,
                               @RequestParam String approved) {

        if ("feature".equals(type)) {

            if(approved.isEmpty()){
                return leaveNoteService.getFutureLeaveNotesByCourseId(courseId);
            }

           return leaveNoteService.getFutureLeaveNotesByCourseIdAndStatus(courseId, approved);
        }

        if("past".equals(type)){

            if(approved.isEmpty()){
                return leaveNoteService.getPastLeaveNotesByCourseId(courseId);
            }

            return leaveNoteService.getPastLeaveNotesByCourseIdAndStatus(courseId, approved);
        }

        if(type.isEmpty()){

            if(approved.isEmpty()){
                return leaveNoteService.getLeaveNotesByCourseId(courseId);
            }

            return leaveNoteService.getLeaveNotesByCourseIdAndStatus(courseId, approved);
        }

        return ResultEntity.Fail(-1, "参数错误");
    }
}